package cn.jdy.plugins.web;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Advice;
import cn.jdy.plugins.service.AdviceService;
/**
 * 市局金点子
 * @author yym
 * @since 18032016
 */

@Controller
@RequestMapping("/advice")
public class AdviceCtrl extends BaseCtrl {
	
	@Autowired
	private AdviceService adviceService;
	@Autowired
	private ConfService confService;
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/index.html")
	public ModelAndView edit() {
		return edit(null);
	}

	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/advice/advice.jsp");
		//襄阳市公安局，需要加入当前时间段周数
		Date datetime = new Date();
		int weekdays = datetime.getDay();
		mv.addObject("weekdays",weekdays);

		Advice advice = new Advice();
		advice.setStatus("1");    //add with daihu 控制前台页面不显示没有审核通过的数据
		advice.setiDisplayLength(2);
		mv.addObject("dataList",this.adviceService.querys(advice));
		mv.addObject("data", id==null||id==0 ? id : this.adviceService.find(id));
		
		// 获得系统所有签收单位
		List<ConfValue> list = confService.queryByType(72,null);
		mv.addObject("deptList", list);
		return mv;
	}


	/**
	 * 保存（更新）文章
	 * @param news
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(Advice advice,Long pid) {
		ModelAndView mv = new ModelAndView("/advice/adviceOk.jsp");
		//将数组转化成String
		advice.setDepars(advice.getUsername());
		/*//将String转化成String
		String[] ds=news.getDepartments();
		String deps = "";
		for(int i=0;i<46;i++){
			if(ds[i]==null || ds[i]=="")ds[i]=";";
			deps += ","+ds[i];
		}
		advice.setDepars(deps);*/
		
		try {
			//如果二级新闻类别为空，则使用一级类别
//		      if(null == advice.getColumnId()) {
//		    	  advice.setColumnId(pid);
//		      }
			if(advice.getId()==null) {// 新增文章
//				advice.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				if(advice.getFalseCreateTime() == null || advice.getFalseCreateTime() == ""){
					advice.setFalseCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				}
				this.adviceService.save(advice);
			} else {//更新文章
				if(advice.getFalseCreateTime() == null || advice.getFalseCreateTime() == ""){
					advice.setFalseCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				}
				this.adviceService.update(advice);
		    // 清理文章缓存
		    NginxPurgeCacheUtil.purge(2, advice.getId());
			}
			// 清理栏目列表缓存
//			NginxPurgeCacheUtil.purge(1, advice.getColumnId());
		} catch(Exception e) {
			BaseLog.e(getClass(), "金点子保存异常！", e);
		}
		return mv;
	}
	
	/**
	 * 投稿文章状态为未审核
	 * @param news
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView queryByPage(Advice advice, String fromDate, String toDate) {
		ModelAndView mv = new ModelAndView("/advice/chooseAdviceList.jsp");
		try {
			//襄阳市公安局，需要加入当前时间段周数
			Date datetime = new Date();
			int weekdays = datetime.getDay();
			advice.setStatus("1");        //add with daihu 控制前台页面不显示没有审核通过的数据
			mv.addObject("weekdays",weekdays);
			
			mv.addObject("dataList",adviceService.query(advice, fromDate, toDate));
			// 设置分页信息
			if (advice.getiDisplayLength() == 0)
				advice.setiDisplayLength(24); // 默认长度24
			if (advice.getPageNo() <= 0)
				advice.setPageNo(1); // 默认页数1
			advice.setiDisplayStart((advice.getPageNo() - 1)
					* advice.getiDisplayLength()); // 显示起始索引
			mv.addObject("pageNo", 1);///
			
			mv.addObject("total",adviceService.count(advice, fromDate, toDate));
			mv.addObject("iTotalRecords",adviceService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return mv;
	}

	/**
	 * 审核
	 * @param id
	 * @return
	 */
	@RequestMapping("/show-{id}.html")
	public ModelAndView edita(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/advice/chooseAdvices.jsp");
		//襄阳市公安局，需要加入当前时间段周数
		Date datetime = new Date();
		int weekdays = datetime.getDay();
		mv.addObject("weekdays",weekdays);
		
		mv.addObject("data", id==null||id==0 ? id : this.adviceService.find(id));
		
		/*mv.addObject("column", this.confService.queryByType(GlobalConstant.TYPE_NEWS, 0L));
//		mv.addObject("data", id==null||id==0 ? id : this.newsService.find(id));
		if(id==null || id==0){
			mv.addObject("data", id);
		}else{
			News news = this.newsService.find(id);
			//投稿统计师将department置null,现将其复原到页面回显
			String[] nd = news.getDepartments();
			if(nd == null){
				String temp=news.getDepars();
				String[] st=temp.split(",");
				int cnt=st.length;
				String[] nds = new String[cnt-1];
				for(int i=1;i<cnt;i++){
					nds[i-1]=st[i];
				}
				news.setDepartments(nds);;
			}
			mv.addObject("data", news);
		}*/
		return mv;
	}

	/**
	 * 保存（审核）金点子
	 * @param news
	 * @return
	 */
	@RequestMapping("/adminSave")
	public ModelAndView savea(Advice advice, Long pid) {
		ModelAndView mv = new ModelAndView("/advice/adviceOk.jsp");
		try {
//			if(advice.getId()==null) {
//				// 新增
//				User user = getUser();
//				advice.setUserid(user.getId());
//				advice.setUsername(user.getUsername());
//				if(advice.getCreateTime() != null && advice.getCreateTime() != ""){
//					advice.setCreateTime(advice.getCreateTime());
//				}else{
//					advice.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
//				}
////				advice.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
//				this.adviceService.save(advice);
//				mv.setViewName(this.viewName.concat("1"));
//			} else {
				//更新
				if(advice.getFalseCreateTime() == null || advice.getFalseCreateTime() == ""){
					advice.setFalseCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				}
				this.adviceService.update(advice);
		    // 清理文章缓存
		    NginxPurgeCacheUtil.purge(2, advice.getId());
//			}
		} catch(Exception e) {
			BaseLog.e(getClass(), "保存异常！", e);
		}

		return mv;
	}
	
	/**
	 * 投稿文章状态为未审核
	 * @param news
	 * @return
	 */
	@RequestMapping("/lists")
	public ModelAndView queryBys(Advice advice, String fromDate, String toDate) {
		ModelAndView mv = new ModelAndView("/advice/advicemore.jsp");
		try {
			//襄阳市公安局，需要加入当前时间段周数
			Date datetime = new Date();
			int weekdays = datetime.getDay();
			mv.addObject("weekdays",weekdays);
			
			// 设置分页信息
			if (advice.getiDisplayLength() == 0)
				advice.setiDisplayLength(5); // 默认长度5
			if (advice.getPageNo() <= 0)
				advice.setPageNo(1); // 默认页数1
			advice.setiDisplayStart((advice.getPageNo() - 1)
					* advice.getiDisplayLength()); // 显示起始索引
			advice.setStatus("1");        //add with daihu 控制前台页面不显示没有审核通过的数据
			mv.addObject("dataList",adviceService.query(advice, fromDate, toDate));
			mv.addObject("pageNo", advice.getPageNo());///
			mv.addObject("size", advice.getiDisplayLength());
			mv.addObject("total",adviceService.count(advice, fromDate, toDate));
			mv.addObject("iTotalRecords",adviceService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return mv;
	}
}
